package com.ksyzt.ksite2.client.site;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.gwt.client.mainframe.AppData;
import com.ksyzt.gwt.client.mainframe.IAppModule;
import com.ksyzt.gwt.client.mainframe.ModuleProperties;
import com.ksyzt.gwt.client.site.urlrewrite.UrlRewriteConfigure;
import com.ksyzt.ksite2.client.log.LogList;

public class SiteFrameModule extends MessageComposite implements IAppModule {

	private static SiteFrameModuleUiBinder uiBinder = GWT
			.create(SiteFrameModuleUiBinder.class);

	interface SiteFrameModuleUiBinder extends UiBinder<Widget, SiteFrameModule> {
	}

	public SiteFrameModule() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiField
	Anchor btnSiteinfo;

	@UiField
	Anchor btnURLRewrite;

	@UiField
	Anchor btnLogger;

	private Widget m_current;

	@UiField
	DockLayoutPanel root;

	com.ksyzt.gwt.client.site.PageSiteInfo m_siteEditor;

	private MessageHandler m_column_editor_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (message == MessageEvent.MESSAGE) {
				message((String) value);
			}

		}
	};

	LogList m_LogList;

	@UiHandler("btnLogger")
	void onLogger(ClickEvent e) {
		if (m_LogList == null) {
			m_LogList = new LogList();

		}
		if (m_current != m_LogList) {
			if (m_current != null) {
				root.remove(m_current);

			}
			root.add(m_LogList);
			m_current = m_LogList;
		}
		setCurrent(m_LogList);
		m_LogList.init();
	}

	void setCurrent(Widget widget) {
		compareWidget(widget, m_LogList);
		compareWidget(widget, m_siteEditor);
		compareWidget(widget, m_rewrite);
	}

	void compareWidget(Widget current, Widget other) {
		if (current.equals(other)) {
			if (other != null) {
				current.removeStyleDependentName(CURRENT);
			}
		} else {
			if (other != null) {
				current.addStyleDependentName(CURRENT);
			}
		}
	}

	@UiHandler("btnSiteinfo")
	void onSite(ClickEvent e) {

		if (m_siteEditor == null) {
			m_siteEditor = new com.ksyzt.gwt.client.site.PageSiteInfo();
			m_siteEditor.addMessageHandler(m_column_editor_handler);

		}
		if (m_current != m_siteEditor) {
			if (m_current != null) {
				root.remove(m_current);

			}
			root.add(m_siteEditor);
			m_current = m_siteEditor;
		}
		m_siteEditor.refresh();
		setCurrent(m_siteEditor);

	}

	private static String CURRENT = "current";
	UrlRewriteConfigure m_rewrite;

	@UiHandler("btnURLRewrite")
	void onRewrite(ClickEvent e) {
		if (m_rewrite == null) {
			m_rewrite = new UrlRewriteConfigure();
			m_rewrite.addMessageHandler(m_column_editor_handler);

		}
		if (m_current != m_rewrite) {
			if (m_current != null) {
				root.remove(m_current);
			}
			root.add(m_rewrite);
			m_current = m_rewrite;
		}
		m_rewrite.init();
		setCurrent(m_rewrite);
	}

	HandlerRegistration handler;

	@Override
	public void onInitialize(AppData application, ModuleProperties pros) {
		handler = this.addMessageHandler(application.messageHandler);
	}

	@Override
	public void onUnInitialize() {
		handler.removeHandler();
	}

	@Override
	public Widget getRootWidget() {
		return this;
	}

}
